﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Me.Prettyprint.Cassandra.Utils
{
    internal static class ConcurrentQueueExtensions
    {
        public static void Clear<T>(this ConcurrentQueue<T> queue)
        {
            T item;
            while (queue.TryDequeue(out item))
            {
                // do nothing
            }
        }
    }
    /// <summary>
    /// FixedSizeConcurrentQueue will push out older stuff when new items
    /// are Enqueued. This duplicates the functionality that Java provides
    /// out of the box.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class FixedSizedConcurrentQueue<T> : ConcurrentQueue<T>
    {
        public int Size { get; private set; }

        public FixedSizedConcurrentQueue(int size)
        {
            Size = size;
        }

        public new void Enqueue(T obj)
        {
            base.Enqueue(obj);
            lock (this)
            {
                while (base.Count > Size)
                {
                    T outObj;
                    base.TryDequeue(out outObj);
                }
            }
        }
    }
}
